
window.onload = () => {
    const closePPBtn = document.getElementById('closePPBtn');

    // 关闭 iframe（悬浮框）
    closePPBtn.addEventListener('click', () => {
        // 告诉父页面关闭 iframe
        window.parent.postMessage('close_popup_frame', '*');
    });
};

document.getElementById("send").addEventListener("click", async () => {
    const keyword = document.getElementById("keyword").value.trim();
    const resultDiv = document.getElementById("result");
    const descriptionDiv = document.getElementById("description");

    if (!keyword) {
        resultDiv.innerHTML = "<p style='color:red;'>请输入关键词</p>";
        descriptionDiv.innerHTML = "";
        return;
    }

    const ollama_server = localStorage.getItem("ollama_server") || "http://127.0.0.1:5000";
    const url = `${ollama_server}/get_pd_info_om?keywords=${encodeURIComponent(keyword)}`;

    resultDiv.innerHTML = "<p>请求中...</p>";
    descriptionDiv.innerHTML = "";

    try {
        const response = await fetch(url);
        const data = await response.json();

        console.log("返回数据：", data);

        let html = "";
        for (const key in data) {
            if (key !== "description") {
                html += `
          <div class="field-block">
            <span class="field-label">${key}：</span>
            <div class="field-content">${data[key]}</div>
          </div>
        `;
            }
        }

        resultDiv.innerHTML = html;

        if (data.description) {
            descriptionDiv.innerHTML = `
        <div class="field-label">description：</div>
        <div class="field-content">${marked.parse(data.description)}</div>
      `;
        }
    } catch (err) {
        console.error("请求失败：", err);
        resultDiv.innerHTML = `<p style='color:red;'>请求失败：${err.message}</p>`;
    }
});


